package com.ws.entity;

import com.ws.dao.BikeDeleteService;
import com.ws.dao.BikeDropService;

import java.util.Scanner;

public class OfOcompany extends SharedBikeCompany implements BikeDropService, BikeDeleteService {
    @Override
    public void deleteBike(SharedBikeCompany company, Scanner scanner) {
        System.out.println("正在从 " + company.getBikeCompanyName() + " 删除单车");
        System.out.println("请输入要删除的单车编号:");
        int bid = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        boolean bikeDeleted = false;
        for (int i = 0; i < company.getSharedBikes().length; i++) {
            SharedBike bike = company.getSharedBikes()[i];
            if (bike.getBid().equals(bid) && bike.getStatus() == 1) { // 只允许删除状态为“可借”的单车
                SharedBike[] newBikes = new SharedBike[company.getSharedBikes().length - 1];
                System.arraycopy(company.getSharedBikes(), 0, newBikes, 0, i);
                System.arraycopy(company.getSharedBikes(), i + 1, newBikes, i, company.getSharedBikes().length - i - 1);
                company.setSharedBikes(newBikes);
                company.setSum(company.getSum() - 1);
                bikeDeleted = true;
                System.out.println("单车删除成功！");
                break;
            }
        }
        if (!bikeDeleted) {
            System.out.println("未找到编号为 " + bid + " 的单车，或单车已被借出，无法删除");
        }
    }
    @Override
    public void dropBike(SharedBikeCompany company, Scanner scanner) {
        System.out.println("正在向 " + company.getBikeCompanyName() + " 投放单车");
        // 获取用户输入的单车信息
        System.out.println("请输入单车编号:");
        int bid;
        if (company.getSharedBikes() != null && company.getSharedBikes().length > 0) {
            bid = company.getSharedBikes()[company.getSharedBikes().length - 1].getBid() + 1;
        } else {
            bid = 1; // 如果数组为空，从1开始编号
        }
        System.out.println("单车编号为: " + bid);

        System.out.println("请输入单车名称:");
        String bname = scanner.next(); // 读取单车名称

        System.out.println("请输入单车状态(0-已借出, 1-可借):");
        int status = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        if (status != 0 && status != 1) {
            System.out.println("无效的单车状态。请输入0（已借出）或1（可借）:");
            return; // 如果输入无效，退出方法
        }

        SharedBike newBike;
        if (status == 0) {
            System.out.println("请输入借出时间 (格式示例: yyyy-MM-dd HH:mm):");
            String borrowTime = scanner.nextLine();
            newBike = SharedBike.createBike(bid, bname, status).setBorrowTime(borrowTime);
        } else {
            newBike = SharedBike.createBike(bid, bname, status);
        }

        // 将新单车添加到数组中
        SharedBike[] newBikes = new SharedBike[company.getSharedBikes().length + 1];
        for (int i = 0; i < company.getSharedBikes().length; i++) {
            newBikes[i] = company.getSharedBikes()[i];
        }
        newBikes[newBikes.length - 1] = newBike;
        company.setSharedBikes(newBikes);
        company.setSum(company.getSum() + 1); // 更新公司单车总量
        System.out.println("单车投放成功！");
    }
}
